package yxy.game.pm2.active.module;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actbeforehand;
import yxy.cherry.data.bean.task;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ActiveModule_十连预抽 extends PlayerModule {

	//bean ----------------------------------------------------
	public static final int acttype = 116;
	
	private int group;	//1,2,3		选择奖励后进行下一轮
	private int id; 		//组的id
	private int getherotype;	//已经领取的英雄组类型
	private ArrayList<Integer> herogroup = new ArrayList<Integer>();	//已经翻牌的英雄组
	private HashMap<Integer, String> idcodemap = new HashMap<Integer, String>();	//key:id  value:任务code
	
	public int getGroup() {
		return group;
	}
	public void setGroup(int group) {
		this.group = group;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getGetherotype() {
		return getherotype;
	}
	public void setGetherotype(int getherotype) {
		this.getherotype = getherotype;
	}
	public ArrayList<Integer> getHerogroup() {
		return herogroup;
	}
	public void setHerogroup(ArrayList<Integer> herogroup) {
		this.herogroup = herogroup;
	}
	public HashMap<Integer, String> getIdcodemap() {
		return idcodemap;
	}
	public void setIdcodemap(HashMap<Integer, String> idcodemap) {
		this.idcodemap = idcodemap;
	}
	
	//func ----------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
//	public static final int nodisplay = 4;	//没翻牌
	
	
	/** 创建ActiveModule_十连预抽
	 * @param pcode
	 */
	public static ActiveModule_十连预抽 create(String pcode) {
		PlayerBean pb = PlayerBean.get(pcode);
		int sid = pb.getServerId();

		ServerBean sb = ServerBean.get(sid);
		int areaid = sb.getAreaId();
		
		activeControl sai = activeControl.get(areaid, acttype);
		if(sai == null) {
			return null;
		}
		
		int group = 1;
		
		ActiveModule_十连预抽 am = new ActiveModule_十连预抽();
		am.setTimeline(sai.getTimeline());
		am.setPlayerCode(pcode);
		am.setGroup(group);
		am.setGetherotype(0);
		
		Map<String, actbeforehand> execlmap = ActDataCenter.getAllData(actbeforehand.class, pcode);
		for(actbeforehand execl : execlmap.values()) {
			
			if(execl.getLayer() == group) {
				am.setId(execl.getId());;
			}
			
			int taskid = execl.getTaskid();
			task taskexecl = DataCenter.getData(taskid, task.class);
			if(taskexecl == null) {
				continue;
			}
			
			Task taskbean = Task.create(pcode, taskexecl);
			am.getIdcodemap().put(execl.getId(), taskbean.getCode());	
		}
		
		am.set();
		return am;
	}

	public void update() {
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		int sid = pb.getServerId();

		ServerBean sb = ServerBean.get(sid);
		int areaid = sb.getAreaId();

		activeControl sai = activeControl.get(areaid, acttype);
		if (sai == null) {
			return;
		}

		// 关闭活动
		if (!sai.running() && running()) {
			setTimeline(sai.getTimeline());
			set();
		}

		// 开启活动
		if (sai.running() && !running()) {

			Task.remove(getPlayerCode(), getIdcodemap().values());
			
			int group = 1;
			
			setTimeline(sai.getTimeline());
			setGroup(group);
			
			Map<String, actbeforehand> execlmap = ActDataCenter.getAllData(actbeforehand.class, getPlayerCode());
			for(actbeforehand execl : execlmap.values()) {
				
				if(execl.getLayer() == group) {
					setId(execl.getId());
				}
				
				int taskid = execl.getTaskid();
				task taskexecl = DataCenter.getData(taskid, task.class);
				if(taskexecl == null) {
					continue;
				}
				
				Task taskbean = Task.create(getPlayerCode(), taskexecl);
				getIdcodemap().put(execl.getId(), taskbean.getCode());	
			}
			
			set();
		}

	}

	public void next() {
		Map<String, actbeforehand> execlmap = ActDataCenter.getAllData(actbeforehand.class, getPlayerCode());
		HashMap<Integer, actbeforehand> groupexeclmap = new HashMap<Integer, actbeforehand>();
		for(actbeforehand execl : execlmap.values()) {
			groupexeclmap.put(execl.getLayer(), execl);
		}
		
		//处理本组任务
		String taskcode = getIdcodemap().get(getId());
		Task taskbean = Task.get(getPlayerCode(), taskcode);
		taskbean.setReward(true);
		taskbean.set();
		
//		int group = getGroup();
		int newgroup = getGroup() + 1;
		if(groupexeclmap.containsKey(newgroup)) {
			//下一组
			actbeforehand execl = groupexeclmap.get(newgroup);
			setGroup(newgroup);
			setGetherotype(0);
			setId(execl.getId());
			setHerogroup(new ArrayList<Integer>());
			set();
		}
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_十连预抽
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_十连预抽 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_十连预抽 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_十连预抽.class);
				}

				if(module != null){module.set();}//同步到redis

				if (module == null) {
					module = create(playerCode);
				}
			}

			module.update();
			module.check();

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//十连预抽兼容
	private static void addAward(ActiveModule_十连预抽 module, String pcode) {
		//旧十连预抽
		HashMap<Integer, String> oldMap = module.getIdcodemap();

		ActiveModule_十连预抽 am = new ActiveModule_十连预抽();
		am.setTimeline(module.getTimeline());
		am.setPlayerCode(pcode);
		if (module.getGroup() < 3){
			am.setGroup(module.getGroup());
			am.setHerogroup(module.getHerogroup());
			am.setId(module.getId());
			am.setGetherotype(module.getGetherotype());
		}else {
			String taskCode = oldMap.get(module.getId());
			Task task = Task.get(pcode, taskCode);
			//奖励已领取
			if (task.isFinish() && task.isReward()){
				am.setGroup(module.getGroup()+1);
				am.setHerogroup(new ArrayList<Integer>());
				am.setId(module.getId()+1);
				am.setGetherotype(0);
			}else {
				//奖励未领取
				am.setGroup(module.getGroup());
				am.setHerogroup(module.getHerogroup());
				am.setId(module.getId());
				am.setGetherotype(module.getGetherotype());
			}
		}

		//新十连预抽
		Map<String, actbeforehand> execlmap = ActDataCenter.getAllData(actbeforehand.class, pcode);
		for(actbeforehand execl : execlmap.values()) {

			if (oldMap.containsKey(execl.getId())){
				am.getIdcodemap().put(execl.getId(), oldMap.get(execl.getId()));
				continue;
			}

			int taskid = execl.getTaskid();
			task taskexecl = DataCenter.getData(taskid, task.class);
			if(taskexecl == null) {
				continue;
			}

			Task taskbean = Task.create(pcode, taskexecl);
			am.getIdcodemap().put(execl.getId(), taskbean.getCode());
		}
		//同步到redis
		am.set();
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		if(running()) {
			Map<String, actbeforehand> execlmap = ActDataCenter.getAllData(actbeforehand.class, getPlayerCode());
			HashMap<Integer, actbeforehand> groupexeclmap = new HashMap<Integer, actbeforehand>();
			for(actbeforehand execl : execlmap.values()) {
				groupexeclmap.put(execl.getLayer(), execl);
			}
			int newgroup = getGroup() + 1;
			if(!groupexeclmap.containsKey(newgroup) && getGetherotype() != 0) {
				new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.领完了);
			}else {
				new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);				
			}
		}else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_十连预抽奖励, false);
			return;
		}
		
		if(getGetherotype() == 0) {
			String taskcode = getIdcodemap().get(getId());
			Task taskbean = Task.get(getPlayerCode(), taskcode);
			if(taskbean != null && taskbean.isFinish() && !taskbean.isReward()) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_十连预抽奖励, true);
				return;
			}
		}

		new TipHandle().updateTip(getPlayerCode(), TipType.活动_十连预抽奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_十连预抽 activeModule_十连预抽 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_十连预抽);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}


}
